﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;

namespace BestBuy.PageGuest
{
    public partial class searchBuyOffers : System.Web.UI.Page
    {
        CBuyerOffer _cbtm = new CBuyerOffer();
        CDanhMuc _cdm = new CDanhMuc();
        string _strSearch = "";
        int _IDCountry = 0;
        int _countProduct = 0;
        private static int _currentPage = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["search"] == null && Request.Params["country"] == null)
            {
                Response.Redirect("~/PageGuest/PageError.aspx");
            }
            _strSearch = Request.Params["search"].ToString();
            _IDCountry = int.Parse(Request.Params["country"].ToString());
            pnEmtry.Visible = false;
            if (!IsPostBack)
            {
                if (_strSearch != "")
                {
                    Filter();
                }
            }
        }

        private void LoadGV()
        {
            txtTrangHienTai.Text = _currentPage.ToString();
            int size = gvListProducts.PageSize;
            int begin = (_currentPage - 1) * size;
            List<BangTinMua> list = _cbtm.Search(_strSearch, _IDCountry, begin, size);
            lbNameProductLinkHeader.Text = _strSearch + " (" + list.Count + " Buy Offers found)";
            if (list.Count > 0)
            {
                _countProduct = list.Count;

                LoadTotalPageNumber(_countProduct); // cap nhat tong so trang cho muc phan trang
                pnEmtry.Visible = false;
                lbGridViewEmtry.Text = "";
                pnPhanTrang.Visible = true;

                gvListProducts.DataSource = list;
                gvListProducts.DataBind();
            }
            else
            {

                pnEmtry.Visible = true;
                pnEmtry.CssClass = "InLeft info message";
                pnPhanTrang.Visible = false;
                gvListProducts.DataSource = null;
                gvListProducts.DataBind();
                lbGridViewEmtry.Text = "There are no data.";

            }
        }

        private void Filter()
        {
            _currentPage = 1;
            LoadGV();
        }

        protected void LoadTotalPageNumber(int productSize)
        {
            int pageSize = _cbtm.slTrangDuLieuOfIDCatalogy(gvListProducts.PageSize, productSize);
            lbTrang.Text = pageSize.ToString();
        }

        public string LoadCount()
        {
            return "( " + _countProduct + " Buy Offers )";
        }

        public string LoadTotalPage()
        {
            return " " + lbTrang.Text + " ";
        }

        public string LoadCurrentPage()
        {
            return " " + _currentPage.ToString() + " ";
        }

        public string LoadPhuongThucThanhToan(object obj)
        {
            string str = "";
            if (obj != null)
            {
                String[] strmang = obj.ToString().Split('+');
                if (strmang[0] == "1")
                {
                    str += "T/T, ";
                }
                if (strmang[1] == "1")
                {
                    str += "L/C, ";
                }
                if (strmang[2] == "1")
                {
                    str += "Western Union, ";
                }
                if (strmang[3] == "1")
                {
                    str += "Paypal, ";
                }
            }
            return str;
        }

        protected void lbtnBegin_Click(object sender, EventArgs e)
        {
            _currentPage = 1;
            LoadGV();
        }

        protected void lbtnPre_Click(object sender, EventArgs e)
        {
            if (_currentPage > 1)
            {
                _currentPage--;
                LoadGV();
            }
        }

        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            if (_currentPage < int.Parse(lbTrang.Text))
            {
                _currentPage++;
                LoadGV();
            }
        }

        protected void lbtnEnd_Click(object sender, EventArgs e)
        {
            _currentPage = int.Parse(lbTrang.Text);
            LoadGV();
        }

        protected void lbtnToi_Click(object sender, EventArgs e)
        {
            int goToPage = Convert.ToInt32(txtTrangHienTai.Text);
            int soLuongTrang = int.Parse(lbTrang.Text);
            if (goToPage > soLuongTrang)
            {
                _currentPage = soLuongTrang;
            }
            else if (goToPage <= soLuongTrang)
            {
                _currentPage = goToPage;
            }
            LoadGV();
        }

        protected void btnHeaderContact_Click(object sender, EventArgs e)
        {
            int size = gvListProducts.Rows.Count;
            CListBuy.list = new List<BangTinMua>();
            for (int i = 0; i <= size - 1; i++)
            {
                try
                {
                    CheckBox pXoa = (CheckBox)gvListProducts.Rows[i].FindControl("chkXoa");
                    if (pXoa.Checked)
                    {
                        BangTinMua sp = new BangTinMua();
                        sp.IDBangTinMua = Convert.ToInt32(gvListProducts.DataKeys[i].Values[0].ToString());
                        sp.TieuDe = gvListProducts.DataKeys[i].Values[1].ToString();
                        sp.IDTaiKhoan = Convert.ToInt32(gvListProducts.DataKeys[i].Values[2].ToString());
                        CListBuy.list.Add(sp);
                    }
                }
                catch
                {
                }
            }
            //chuyen sang trang contact voi doi tuong pid=0 -> danh cho nhiu doi tuong
            Response.Redirect("~/PageGuest/PageContactMessage.aspx?ml=2&pid=0");
        }
    }
}